package com.logou.utils;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 分页工具
 */
public class PageableUtil {
    private static int pageNumber;
    private static int pageSize=4;
    public static Pageable getPageable() {
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        pageNumber= StringUtils.isEmpty(page) ? 0 : Integer.parseInt(page)-1;
        pageSize= StringUtils.isEmpty(size) ? pageSize : Integer.parseInt(size);
        return PageRequest.of(pageNumber,pageSize);
    }
}
